using System;
using System.Collections.Generic;
using System.Text;

namespace GenerateP4SpecForm.Enums
{
    public static class EnumHelper
    {
        /// <summary>
        /// Returns the name of the given enum
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetName(object enumValue)
        {
            return Enum.GetName(enumValue.GetType(), enumValue);
        }

        /// <summary>
        /// Returns the enum for the given enum name string, ignores case
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumName"></param>
        /// <returns></returns>
        public static T GetEnumFromName<T>(string enumName)
        {
            return GetEnumFromName<T>(enumName, true);
        }

        /// <summary>
        /// Returns the enum for the given enum name string
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumName"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static T GetEnumFromName<T>(string enumName, bool ignoreCase)
        {
            T enumValue = default(T);
            foreach (string name in Enum.GetNames(typeof(T)))
            {
                if (name.Equals(enumName) || (ignoreCase && name.ToUpper().Equals(enumName.ToUpper())))
                {
                    enumValue = (T)Enum.Parse(typeof(T), name);
                    break;
                }
            }
            return enumValue;
        }
    }
}
